﻿Public Interface IStringPropertyDict
    Property content As IContent
    Function getValues() As IEnumerable(Of String)
    Function getLabelFor(value As String) As String
    ReadOnly Property default_value As String
End Interface

''' <summary>
''' provide the template by decorating a dictonary
''' </summary>
''' <remarks></remarks>
Public Class BaseStringSelection
    Implements IStringPropertyDict

    Protected _dictonary As New Dictionary(Of String, String)

    Public Sub New()
        setUpDictornary()
    End Sub

    Public Function getLabelFor(value As String) As String Implements IStringPropertyDict.getLabelFor
        If value Is Nothing Then Return String.Empty
        Return If(_dictonary.ContainsKey(value), _dictonary(value), value)
    End Function

    'overide to provide the right order
    Public Overridable Function getValues() As System.Collections.Generic.IEnumerable(Of String) Implements IStringPropertyDict.getValues

        Return _dictonary.Keys
    End Function

    ''' <summary>
    ''' overrdide and setup dictory
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overridable Sub setUpDictornary()

    End Sub

    Public Property content As IContent Implements IStringPropertyDict.content

    Public Overridable ReadOnly Property default_value As String Implements IStringPropertyDict.default_value
        Get
            Dim result As String = getValues().FirstOrDefault
            Return If(result Is Nothing, String.Empty, result)
        End Get
    End Property
End Class


Public Class StringPropertyDictAttr
    Inherits MysteryPropertyAttribute

    Public Property implementing_type As Type

    Private _activator As IActivator

    Public Overrides Sub setUp()

        If implementing_type IsNot Nothing Then
            implementing_type.GetInterfaceMap(GetType(IStringPropertyDict)) 'crash if not right
        Else
            implementing_type = GetType(BaseStringSelection)
        End If

        _activator = getGlobalObject(Of FastActivator).getActivator(implementing_type)
    End Sub

    Public Function getDictionary(content As IContent) As IStringPropertyDict
        Dim result As IStringPropertyDict = _activator.createInstance()
        result.content = content
        Return result
    End Function

    
End Class
